package cn.stylefeng.guns.modular.demo.controller;


import cn.stylefeng.guns.dao.entity.*;
import cn.stylefeng.guns.dao.service.*;
import cn.stylefeng.guns.dao.service.impl.ImportViewServiceImpl;
import cn.stylefeng.guns.modular.demo.controller.domain.peisong.ExcelTitleQueryRequest;
import cn.stylefeng.guns.modular.demo.controller.domain.peisong.ImportViewQueryRequest;
import cn.stylefeng.guns.modular.demo.controller.domain.peisong.PeisongQueryRequest;
import cn.stylefeng.guns.modular.demo.excel.domain.RealTimeInventoryExcelVO;
import cn.stylefeng.guns.modular.demo.excel.domain.asilikang.DistributionDocDetailExcelVO;
import cn.stylefeng.guns.modular.demo.excel.domain.asilikang.DistributionDocExcelVO;
import cn.stylefeng.guns.modular.demo.excel.domain.asilikang.SellDocExcelDetailVO;
import cn.stylefeng.guns.modular.demo.excel.domain.asilikang.SellDocExcelVO;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.rule.enums.ResBizTypeEnum;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Maps;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author quspacedraon
 * @since 2024-01-25
 */
@RestController
@Slf4j
@ApiResource(name = "报表相关", resBizType = ResBizTypeEnum.BUSINESS, path = "/peisong")
public class PeisongController {
    @Resource
    private IDistributionDocService distributionDocService;
    @Resource
    private IDistributionDocDetailService distributionDocDetailService;
    @Resource
    private ISellDocService sellDocService;
    @Resource
    private ISellDocDetailService sellDocDetailService;
    @Resource
    private IImportViewService importViewService;

    @PostResource(name = "列表头", path = "/peisong/title/list", requiredLogin = true, requiredPermission = true)
    @ApiOperation("列表头")
    public ResponseData<Map<String, String>> peisongTtileList(@RequestBody ExcelTitleQueryRequest excelTitleQueryRequest) {
        String type = excelTitleQueryRequest.getType();
        Class clazz = null;
        if ("1".equals(type)) {
            clazz = DistributionDocExcelVO.class;
        } else if ("2".equals(type)) {
            clazz = DistributionDocDetailExcelVO.class;
        } else if ("3".equals(type)) {
            clazz = SellDocExcelVO.class;
        } else if ("4".equals(type)) {
            clazz = SellDocExcelDetailVO.class;
        } else if ("realTimeInventory".equals(type)) {
            clazz = RealTimeInventoryExcelVO.class;
        }
        return new SuccessResponseData<>(convertToMap(clazz));
    }


    @PostResource(name = "配送单据", path = "/peisong/data/peisongdanju", requiredLogin = true, requiredPermission = true)
    @ApiOperation("配送单据")
    public ResponseData<PageResult<DistributionDocExcelVO>> peisongDanju(@RequestBody PeisongQueryRequest peisongQueryRequest) {
        List<String> goodsCodeList = peisongQueryRequest.getGoodsCodeList();
        Long timeEnd = peisongQueryRequest.getTimeEnd();
        Long timeBegin = peisongQueryRequest.getTimeBegin();
        List<String> senderCodeList = peisongQueryRequest.getSenderCodeList();
        List<String> stoCodeList = peisongQueryRequest.getStoCodeList();
        IPage<DistributionDoc> page = new Page<>(peisongQueryRequest.getPageNo(), peisongQueryRequest.getPageSize(), false);
        page = distributionDocService.page(page, Wrappers.<DistributionDoc>lambdaQuery()
                .in(CollectionUtils.isNotEmpty(stoCodeList), DistributionDoc::getStoCode, peisongQueryRequest.getStoCodeList())
                .in(CollectionUtils.isNotEmpty(senderCodeList), DistributionDoc::getSenderCode, peisongQueryRequest.getSenderCodeList())
                .in(CollectionUtils.isNotEmpty(goodsCodeList), DistributionDoc::getGoodsCode, peisongQueryRequest.getGoodsCodeList())
                .ge(Objects.nonNull(timeBegin), DistributionDoc::getTime, timeBegin)
                .le(Objects.nonNull(timeEnd), DistributionDoc::getTime, timeEnd)
        );

        DistributionDoc one = distributionDocService.getOne(Wrappers.<DistributionDoc>query()
                .select("count(1) as num")
                .in(CollectionUtils.isNotEmpty(stoCodeList), "sto_code", peisongQueryRequest.getStoCodeList())
                .in(CollectionUtils.isNotEmpty(senderCodeList), "sender_code", peisongQueryRequest.getSenderCodeList())
                .in(CollectionUtils.isNotEmpty(goodsCodeList), "goods_code", peisongQueryRequest.getGoodsCodeList())
                .ge(Objects.nonNull(timeBegin), "time", timeBegin)
                .le(Objects.nonNull(timeEnd), "time", timeEnd)
        );
        page.setTotal(Optional.ofNullable(one).map(r -> r.getNum()).orElse(0));
        List<DistributionDocExcelVO> collect = page.getRecords().stream().map(r -> {
            String data = r.getData();
            return JSON.parseObject(data, DistributionDocExcelVO.class);
        }).collect(Collectors.toList());

        return new SuccessResponseData<>(PageResultFactory.createPageResult(collect, page.getTotal(), (int) page.getSize(), (int) page.getCurrent()));
    }

//    @PostResource(name = "配送单据统计", path = "/peisong/data/peisongdanju/sum", requiredLogin = true, requiredPermission = true)
//    @ApiOperation("配送单据统计")
//    public ResponseData<DistributionDoc> peisongSumDanju(@RequestBody PeisongQueryRequest peisongQueryRequest) {
//        List<String> goodsCodeList = peisongQueryRequest.getGoodsCodeList();
//        Long timeEnd = peisongQueryRequest.getTimeEnd();
//        Long timeBegin = peisongQueryRequest.getTimeBegin();
//        List<String> senderCodeList = peisongQueryRequest.getSenderCodeList();
//        List<String> stoCodeList = peisongQueryRequest.getStoCodeList();
//
//
//        DistributionDoc one = distributionDocService.getOne(Wrappers.<DistributionDoc>query()
//                .select("sum(num) as num ,sum(price1) as price1,sum(price2) as price2 ,sum(price3) as price3 ,sum(price4) as price4 ,sum(price5) as price5,sum(price6) as price6,sum(price7) as price7,sum(price8) as price8")
//                .in(CollectionUtils.isNotEmpty(stoCodeList), "sto_code", peisongQueryRequest.getStoCodeList())
//                .in(CollectionUtils.isNotEmpty(senderCodeList), "sender_code", peisongQueryRequest.getSenderCodeList())
//                .in(CollectionUtils.isNotEmpty(goodsCodeList), "goods_code", peisongQueryRequest.getGoodsCodeList())
//                .ge(Objects.nonNull(timeBegin), "time", timeBegin)
//                .le(Objects.nonNull(timeEnd), "time", timeEnd)
//        );
//        if (Objects.isNull(one)) {
//            one = new DistributionDoc();
//        }
//        return new SuccessResponseData<>(one);
//    }


    @PostResource(name = "配送单据-明细", path = "/peisong/data/peisongdanju/detail", requiredLogin = true, requiredPermission = true)
    @ApiOperation("配送单据-明细")
    public ResponseData<PageResult<DistributionDocDetailExcelVO>> peisongDanjuDetail(@RequestBody PeisongQueryRequest peisongQueryRequest) {
        List<String> goodsCodeList = peisongQueryRequest.getGoodsCodeList();
        Long timeEnd = peisongQueryRequest.getTimeEnd();
        Long timeBegin = peisongQueryRequest.getTimeBegin();
        List<String> senderCodeList = peisongQueryRequest.getSenderCodeList();
        List<String> stoCodeList = peisongQueryRequest.getStoCodeList();
        IPage<DistributionDocDetail> page = new Page<>(peisongQueryRequest.getPageNo(), peisongQueryRequest.getPageSize(), false);
        page = distributionDocDetailService.page(page, Wrappers.<DistributionDocDetail>lambdaQuery()
                .in(CollectionUtils.isNotEmpty(stoCodeList), DistributionDocDetail::getStoCode, peisongQueryRequest.getStoCodeList())
                .in(CollectionUtils.isNotEmpty(senderCodeList), DistributionDocDetail::getSenderCode, peisongQueryRequest.getSenderCodeList())
                .in(CollectionUtils.isNotEmpty(goodsCodeList), DistributionDocDetail::getGoodsCode, peisongQueryRequest.getGoodsCodeList())
                .ge(Objects.nonNull(timeBegin), DistributionDocDetail::getTime, timeBegin)
                .le(Objects.nonNull(timeEnd), DistributionDocDetail::getTime, timeEnd)
        );
        DistributionDocDetail one = distributionDocDetailService.getOne(Wrappers.<DistributionDocDetail>query()
                .select("count(1) as num")
                .in(CollectionUtils.isNotEmpty(stoCodeList), "sto_code", peisongQueryRequest.getStoCodeList())
                .in(CollectionUtils.isNotEmpty(senderCodeList), "sender_code", peisongQueryRequest.getSenderCodeList())
                .in(CollectionUtils.isNotEmpty(goodsCodeList), "goods_code", peisongQueryRequest.getGoodsCodeList())
                .ge(Objects.nonNull(timeBegin), "time", timeBegin)
                .le(Objects.nonNull(timeEnd), "time", timeEnd)
        );
        page.setTotal(Optional.ofNullable(one).map(r -> r.getNum()).orElse(0));
        List<DistributionDocDetailExcelVO> collect = page.getRecords().stream().map(r -> {
            String data = r.getData();
            return JSON.parseObject(data, DistributionDocDetailExcelVO.class);
        }).collect(Collectors.toList());

        return new SuccessResponseData<>(PageResultFactory.createPageResult(collect, page.getTotal(), (int) page.getSize(), (int) page.getCurrent()));
    }


    @PostResource(name = "销售单据", path = "/peisong/data/xiaosoudanju", requiredLogin = true, requiredPermission = true)
    @ApiOperation("销售单据")
    public ResponseData<PageResult<SellDocExcelVO>> sellDoc(@RequestBody PeisongQueryRequest peisongQueryRequest) {
        List<String> goodsCodeList = peisongQueryRequest.getGoodsCodeList();
        Long timeEnd = peisongQueryRequest.getTimeEnd();
        Long timeBegin = peisongQueryRequest.getTimeBegin();
        List<String> stoCodeList = peisongQueryRequest.getStoCodeList();
        IPage<SellDoc> page = new Page<>(peisongQueryRequest.getPageNo(), peisongQueryRequest.getPageSize(), false);
        page = sellDocService.page(page, Wrappers.<SellDoc>lambdaQuery()
                .in(CollectionUtils.isNotEmpty(stoCodeList), SellDoc::getStoCode, peisongQueryRequest.getStoCodeList())
                .in(CollectionUtils.isNotEmpty(goodsCodeList), SellDoc::getGoodsCode, peisongQueryRequest.getGoodsCodeList())
                .ge(Objects.nonNull(timeBegin), SellDoc::getTime, timeBegin)
                .le(Objects.nonNull(timeEnd), SellDoc::getTime, timeEnd)
        );
        SellDoc one = sellDocService.getOne(Wrappers.<SellDoc>query()
                .select("count(1) as 客品数")
                .in(CollectionUtils.isNotEmpty(stoCodeList), "sto_code", peisongQueryRequest.getStoCodeList())
                .in(CollectionUtils.isNotEmpty(goodsCodeList), "goods_code", peisongQueryRequest.getGoodsCodeList())
                .ge(Objects.nonNull(timeBegin), "time", timeBegin)
                .le(Objects.nonNull(timeEnd), "time", timeEnd)
        );
        page.setTotal(Optional.ofNullable(one).map(r -> r.get客品数()).orElse(0));
        List<SellDocExcelVO> collect = page.getRecords().stream().map(r -> {
            String data = r.getData();
            return JSON.parseObject(data, SellDocExcelVO.class);
        }).collect(Collectors.toList());

        return new SuccessResponseData<>(PageResultFactory.createPageResult(collect, page.getTotal(), (int) page.getSize(), (int) page.getCurrent()));
    }

    @PostResource(name = "销售单据明细", path = "/peisong/data/xiaosoudanju/detail", requiredLogin = true, requiredPermission = true)
    @ApiOperation("销售单据明细")
    public ResponseData<PageResult<SellDocExcelDetailVO>> sellDocDetail(@RequestBody PeisongQueryRequest peisongQueryRequest) {
        List<String> goodsCodeList = peisongQueryRequest.getGoodsCodeList();
        Long timeEnd = peisongQueryRequest.getTimeEnd();
        Long timeBegin = peisongQueryRequest.getTimeBegin();
        List<String> stoCodeList = peisongQueryRequest.getStoCodeList();
        IPage<SellDocDetail> page = new Page<>(peisongQueryRequest.getPageNo(), peisongQueryRequest.getPageSize(), false);
        LambdaQueryWrapper<SellDocDetail> le = Wrappers.<SellDocDetail>lambdaQuery()
                .in(CollectionUtils.isNotEmpty(stoCodeList), SellDocDetail::getStoCode, peisongQueryRequest.getStoCodeList())
                .in(CollectionUtils.isNotEmpty(goodsCodeList), SellDocDetail::getGoodsCode, peisongQueryRequest.getGoodsCodeList())
                .ge(Objects.nonNull(timeBegin), SellDocDetail::getTime, timeBegin)
                .le(Objects.nonNull(timeEnd), SellDocDetail::getTime, timeEnd);

        SellDocDetail one = sellDocDetailService.getOne(Wrappers.<SellDocDetail>query()
                .select("count(1) as num")
                .in(CollectionUtils.isNotEmpty(stoCodeList), "sto_code", peisongQueryRequest.getStoCodeList())
                .in(CollectionUtils.isNotEmpty(goodsCodeList), "goods_code", peisongQueryRequest.getGoodsCodeList())
                .ge(Objects.nonNull(timeBegin), "time", timeBegin)
                .le(Objects.nonNull(timeEnd), "time", timeEnd)
        );
        page.setTotal(Optional.ofNullable(one).map(r -> r.getNum()).orElse(0));
        page = sellDocDetailService.page(page, le);
        List<SellDocExcelDetailVO> collect = page.getRecords().stream().map(r -> {
            String data = r.getData();
            return JSON.parseObject(data, SellDocExcelDetailVO.class);
        }).collect(Collectors.toList());

        return new SuccessResponseData<>(PageResultFactory.createPageResult(collect, page.getTotal(), (int) page.getSize(), (int) page.getCurrent()));
    }

//    @PostResource(name = "配送单据统计", path = "/peisong/data/peisongdanju/sum", requiredLogin = true, requiredPermission = true)
//    @ApiOperation("配送单据统计")
//    public ResponseData<DistributionDoc> peisongSumDanju(@RequestBody PeisongQueryRequest peisongQueryRequest) {
//        List<String> goodsCodeList = peisongQueryRequest.getGoodsCodeList();
//        Long timeEnd = peisongQueryRequest.getTimeEnd();
//        Long timeBegin = peisongQueryRequest.getTimeBegin();
//        List<String> senderCodeList = peisongQueryRequest.getSenderCodeList();
//        List<String> stoCodeList = peisongQueryRequest.getStoCodeList();
//
//
//        DistributionDoc one = distributionDocService.getOne(Wrappers.<DistributionDoc>query()
//                .select("sum(num) as num ,sum(price1) as price1,sum(price2) as price2 ,sum(price3) as price3 ,sum(price4) as price4 ,sum(price5) as price5,sum(price6) as price6,sum(price7) as price7,sum(price8) as price8")
//                .in(CollectionUtils.isNotEmpty(stoCodeList), "sto_code", peisongQueryRequest.getStoCodeList())
//                .in(CollectionUtils.isNotEmpty(senderCodeList), "sender_code", peisongQueryRequest.getSenderCodeList())
//                .in(CollectionUtils.isNotEmpty(goodsCodeList), "goods_code", peisongQueryRequest.getGoodsCodeList())
//                .ge(Objects.nonNull(timeBegin), "time", timeBegin)
//                .le(Objects.nonNull(timeEnd), "time", timeEnd)
//        );
//        if (Objects.isNull(one)) {
//            one = new DistributionDoc();
//        }
//        return new SuccessResponseData<>(one);
//    }


//    @PostResource(name = "配送单据-明细", path = "/peisong/data/peisongdanju/detail", requiredLogin = true, requiredPermission = true)
//    @ApiOperation("配送单据-明细")
//    public ResponseData<PageResult<DistributionDocDetailExcelVO>> peisongDanjuDetail(@RequestBody PeisongQueryRequest peisongQueryRequest) {
//        List<String> goodsCodeList = peisongQueryRequest.getGoodsCodeList();
//        Long timeEnd = peisongQueryRequest.getTimeEnd();
//        Long timeBegin = peisongQueryRequest.getTimeBegin();
//        List<String> senderCodeList = peisongQueryRequest.getSenderCodeList();
//        List<String> stoCodeList = peisongQueryRequest.getStoCodeList();
//        IPage<DistributionDocDetail> page = new Page<>(peisongQueryRequest.getPageNo(), peisongQueryRequest.getPageSize());
//        page = distributionDocDetailService.page(page, Wrappers.<DistributionDocDetail>lambdaQuery()
//                .in(CollectionUtils.isNotEmpty(stoCodeList), DistributionDocDetail::getStoCode, peisongQueryRequest.getStoCodeList())
//                .in(CollectionUtils.isNotEmpty(senderCodeList), DistributionDocDetail::getSenderCode, peisongQueryRequest.getSenderCodeList())
//                .in(CollectionUtils.isNotEmpty(goodsCodeList), DistributionDocDetail::getGoodsCode, peisongQueryRequest.getGoodsCodeList())
//                .ge(Objects.nonNull(timeBegin), DistributionDocDetail::getTime, timeBegin)
//                .le(Objects.nonNull(timeEnd), DistributionDocDetail::getTime, timeEnd)
//        );
//        List<DistributionDocDetailExcelVO> collect = page.getRecords().stream().map(r -> {
//            String data = r.getData();
//            return JSON.parseObject(data, DistributionDocDetailExcelVO.class);
//        }).collect(Collectors.toList());
//
//        return new SuccessResponseData<>(PageResultFactory.createPageResult(collect, page.getTotal(), (int) page.getSize(), (int) page.getCurrent()));
//    }


    @PostResource(name = "配送单据明细统计", path = "/peisong/data/peisongdanju/detail/sum", requiredLogin = true, requiredPermission = true)
    @ApiOperation("配送单据明细统计")
    public ResponseData<DistributionDocDetail> peisongSumDanjuDetail(@RequestBody PeisongQueryRequest peisongQueryRequest) {
        List<String> goodsCodeList = peisongQueryRequest.getGoodsCodeList();
        Long timeEnd = peisongQueryRequest.getTimeEnd();
        Long timeBegin = peisongQueryRequest.getTimeBegin();
        List<String> senderCodeList = peisongQueryRequest.getSenderCodeList();
        List<String> stoCodeList = peisongQueryRequest.getStoCodeList();

        DistributionDocDetail one = distributionDocDetailService.getOne(Wrappers.<DistributionDocDetail>query()
                .select("sum(num) as num ,sum(price1) as price1,sum(price2) as price2 ,sum(price3) as price3 ,sum(price4) as price4 ,sum(price5) as price5,sum(price6) as price6,sum(price7) as price7")
                .in(CollectionUtils.isNotEmpty(stoCodeList), "sto_code", peisongQueryRequest.getStoCodeList())
                .in(CollectionUtils.isNotEmpty(senderCodeList), "sender_code", peisongQueryRequest.getSenderCodeList())
                .in(CollectionUtils.isNotEmpty(goodsCodeList), "goods_code", peisongQueryRequest.getGoodsCodeList())
                .ge(Objects.nonNull(timeBegin), "time", timeBegin)
                .le(Objects.nonNull(timeEnd), "time", timeEnd)
        );
        if (Objects.isNull(one)) {
            one = new DistributionDocDetail();
        }
        return new SuccessResponseData<>(one);
    }

    @PostResource(name = "配送单据统计", path = "/peisong/data/peisongdanju/sum", requiredLogin = true, requiredPermission = true)
    @ApiOperation("配送单据明细统计")
    public ResponseData<DistributionDoc> peisongSumDanju(@RequestBody PeisongQueryRequest peisongQueryRequest) {
        List<String> goodsCodeList = peisongQueryRequest.getGoodsCodeList();
        Long timeEnd = peisongQueryRequest.getTimeEnd();
        Long timeBegin = peisongQueryRequest.getTimeBegin();
        List<String> senderCodeList = peisongQueryRequest.getSenderCodeList();
        List<String> stoCodeList = peisongQueryRequest.getStoCodeList();

        DistributionDoc one = distributionDocService.getOne(Wrappers.<DistributionDoc>query()
                .select("sum(num) as num ,sum(price1) as price1,sum(price2) as price2 ,sum(price3) as price3 ,sum(price4) as price4 ,sum(price5) as price5,sum(price6) as price6,sum(price7) as price7,sum(price8) as price8")
                .in(CollectionUtils.isNotEmpty(stoCodeList), "sto_code", peisongQueryRequest.getStoCodeList())
                .in(CollectionUtils.isNotEmpty(senderCodeList), "sender_code", peisongQueryRequest.getSenderCodeList())
                .in(CollectionUtils.isNotEmpty(goodsCodeList), "goods_code", peisongQueryRequest.getGoodsCodeList())
                .ge(Objects.nonNull(timeBegin), "time", timeBegin)
                .le(Objects.nonNull(timeEnd), "time", timeEnd)
        );
        if (Objects.isNull(one)) {
            one = new DistributionDoc();
        }
        return new SuccessResponseData<>(one);
    }


    @PostResource(name = "销售单据统计", path = "/peisong/data/xiaosoudanju/sum", requiredLogin = true, requiredPermission = true)
    @ApiOperation("销售单据统计")
    public ResponseData<Map<String, BigDecimal>> xiaoshouSumDanju(@RequestBody PeisongQueryRequest peisongQueryRequest) {
        List<String> goodsCodeList = peisongQueryRequest.getGoodsCodeList();
        Long timeEnd = peisongQueryRequest.getTimeEnd();
        Long timeBegin = peisongQueryRequest.getTimeBegin();
        List<String> stoCodeList = peisongQueryRequest.getStoCodeList();

        SellDoc one = sellDocService.getOne(Wrappers.<SellDoc>query()
                .select("sum(应收金额) as 应收金额,sum(实收金额) as 实收金额")
                .in(CollectionUtils.isNotEmpty(stoCodeList), "sto_code", peisongQueryRequest.getStoCodeList())
                .in(CollectionUtils.isNotEmpty(goodsCodeList), "goods_code", peisongQueryRequest.getGoodsCodeList())
                .ge(Objects.nonNull(timeBegin), "time", timeBegin)
                .le(Objects.nonNull(timeEnd), "time", timeEnd)
        );
        HashMap<String, BigDecimal> objectObjectHashMap = Maps.newHashMap();
        if (Objects.isNull(one)) {
            objectObjectHashMap.put("num", BigDecimal.ZERO);
            objectObjectHashMap.put("num", BigDecimal.ZERO);
        } else {
            objectObjectHashMap.put("price1", one.get应收金额());
            objectObjectHashMap.put("price2", one.get实收金额());
        }
        return new SuccessResponseData<>(objectObjectHashMap);
    }

    @PostResource(name = "销售单据明细统计", path = "/peisong/data/xiaosoudanju/detail/sum", requiredLogin = true, requiredPermission = true)
    @ApiOperation("销售单据明细统计")
    public ResponseData<SellDocDetail> xiaoshouDetailSumDanju(@RequestBody PeisongQueryRequest peisongQueryRequest) {
        List<String> goodsCodeList = peisongQueryRequest.getGoodsCodeList();
        Long timeEnd = peisongQueryRequest.getTimeEnd();
        Long timeBegin = peisongQueryRequest.getTimeBegin();
        List<String> stoCodeList = peisongQueryRequest.getStoCodeList();
        List<String> senderCodeList = peisongQueryRequest.getSenderCodeList();

        SellDocDetail one = sellDocDetailService.getOne(Wrappers.<SellDocDetail>query()
                .select("sum(num) as num ,sum(price1) as price1,sum(price2) as price2")
                .in(CollectionUtils.isNotEmpty(stoCodeList), "sto_code", peisongQueryRequest.getStoCodeList())
                .in(CollectionUtils.isNotEmpty(goodsCodeList), "goods_code", peisongQueryRequest.getGoodsCodeList())
                .in(CollectionUtils.isNotEmpty(senderCodeList), "sender_code", peisongQueryRequest.getSenderCodeList())
                .ge(Objects.nonNull(timeBegin), "time", timeBegin)
                .le(Objects.nonNull(timeEnd), "time", timeEnd)
        );
        if (Objects.isNull(one)) {
            one = new SellDocDetail();
        }
        return new SuccessResponseData<>(one);
    }


    @PostResource(name = "报表展示", path = "/peisong/data/import-view", requiredLogin = true, requiredPermission = true)
    @ApiOperation("报表展示")
    public ResponseData<PageResult<RealTimeInventoryExcelVO>> realTimeInventoryList(@RequestBody ImportViewQueryRequest importViewQueryRequest) {
        IPage<ImportView> page = new Page<>(importViewQueryRequest.getPageNo(), importViewQueryRequest.getPageSize(), false);
        LambdaQueryWrapper<ImportView> importViewLambdaQueryWrapper = Wrappers.<ImportView>lambdaQuery();
        page = importViewService.page(page, buildWrappers(importViewLambdaQueryWrapper, importViewQueryRequest));

        LambdaQueryWrapper<ImportView> lambda = Wrappers.<ImportView>query()
                .select("count(1) as num1").lambda();
        ImportView one = importViewService.getOne(buildWrappers(lambda, importViewQueryRequest));
        page.setTotal(Optional.ofNullable(one).map(ImportView::getNum1).orElse(0));
        List<RealTimeInventoryExcelVO> collect = page.getRecords().stream().map(r -> {
            String data = r.getInfo();
            return JSON.parseObject(data, RealTimeInventoryExcelVO.class);
        }).collect(Collectors.toList());

        return new SuccessResponseData<>(PageResultFactory.createPageResult(collect, page.getTotal(), (int) page.getSize(), (int) page.getCurrent()));
    }

    @PostResource(name = "报表数据统计", path = "/peisong/data/xiaosoudanju/detail/import-view", requiredLogin = true, requiredPermission = true)
    @ApiOperation("报表数据统计")
    public ResponseData<ImportView> realTimeInventoryListStatis(@RequestBody ImportViewQueryRequest peisongQueryRequest) {
        QueryWrapper<ImportView> select = Wrappers.<ImportView>query()
                .select("sum(num1) as num1");
        ImportView one = importViewService.getOne(buildWrappers(select.lambda(), peisongQueryRequest));
        if (Objects.isNull(one)) {
            one = new ImportView();
        }
        return new SuccessResponseData<>(one);
    }

    /**
     * 构建查询条件
     *
     * @param importViewLambdaQueryWrapper
     * @param importViewQueryRequest
     * @return
     */
    private LambdaQueryWrapper buildWrappers(LambdaQueryWrapper<ImportView> importViewLambdaQueryWrapper, ImportViewQueryRequest importViewQueryRequest) {
        List<String> proCodeList = importViewQueryRequest.getProCodeList();
        List<String> stoCodeList = importViewQueryRequest.getStoCodeList();
        String templateType = importViewQueryRequest.getTemplateType();
        List<String> str1List = importViewQueryRequest.getStr1();
        importViewLambdaQueryWrapper.in(CollectionUtils.isNotEmpty(stoCodeList), ImportView::getStoCode, stoCodeList)
                .in(CollectionUtils.isNotEmpty(proCodeList), ImportView::getProCode, proCodeList)
                .in(CollectionUtils.isNotEmpty(str1List), ImportView::getStr1, str1List)
                .in(ImportView::getTemplateType, templateType);
        return importViewLambdaQueryWrapper;
    }

    public static void main(String[] args) {
//        Map<String, String> stringStringMap = convertToMap();
//        System.out.println(JSON.toJSONString(stringStringMap));
    }

    public static Map<String, String> convertToMap(Class clazz) {
        Map<String, String> map = new HashMap<>();
        Field[] fields = clazz.getDeclaredFields();

        for (Field field : fields) {
            if (field.isAnnotationPresent(ExcelProperty.class)) {
                ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class);
                map.put(field.getName(), excelProperty.value()[0]);
            }
        }

        return map;
    }


}
